<template>
  <div>
    <el-dialog title="打印预览" v-model="showprint" width="920px" :append-to-body="true" @close="showprint = false"
      @opened="opendialog">
      <el-divider></el-divider>
      <!-- 1.销售单 -->
      <div id="printArea" ref="printArea" style=" position: relative;">
        <div v-if="ssakeorderis == 33" ref="priqrcodes" style="
          position:absolute;
          width: 100px;
          height: 100px;
          top: 30px;
          left: 84%;
          ">
        </div>
        <div>
          <div style="height: 15px"></div>
          <div class="printcss">
            <div style="
                width: 100%;
                font-size: 30px;
                text-align: center;
                font-weight: 500;
                display: flex;
                align-items: center;
                justify-content: center;
                color: black;
                padding: 0 10px;
                margin-top: 15px;
                font-family: Microsoft YaHei;
                margin-bottom: 25px;
              ">
              <div>{{ componyname }}</div>
              <div style="margin-left: 25px">{{ ordername }}</div>
            </div>
            <div class="printcss-top"></div>
          </div>

          <div style="width: 100%; margin-bottom: 10px; padding: 0 10px">
            <div style="width: 100%; display: flex; align-items: center">
              <div v-if="
                  ordershowid == 1 ||
                  ordershowid == 2 ||
                  ordershowid == 6 ||
                  ordershowid == 20
                " style="
                  width: 31%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                发货仓库：{{ salesorder.storeName }}
              </div>
              <div v-if="ordershowid == 15 || ordershowid == 16" style="
                  width: 31%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                初始仓：{{ salesorder.oldStoreName }}
              </div>

              <div v-if="
                  ordershowid == 1 ||
                  ordershowid == 20 ||
                  ordershowid == 2 ||
                  ordershowid == 3 ||
                  ordershowid == 11 ||
                  ordershowid == 5 ||
                  ordershowid == 6 ||
                  ordershowid == 8 ||
                  ordershowid == 9 ||
                  ordershowid == 15 ||
                  ordershowid == 16
                " style="
                  width: 60%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                <span class="label-style">单据日期</span>：{{ salesorder.orderDate }}
              </div>

              <div v-if="ordershowid === 5 || ordershowid === 9" style="
                  width: 40%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                <span class="label-style">单据编号</span>：{{ salesorder.orderNo }}
              </div>

            </div>
            <div style="
                width: 100%;
                display: flex;
                align-items: center;
                margin: 10px auto;
                margin-bottom: 0;
              ">
              <div v-if="
                  ordershowid == 1 ||
                  ordershowid == 2 ||
                  ordershowid == 6 ||
                  ordershowid == 20
                " style="
                  width: 31%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                <span class="label-style">公司名称</span>：{{ salesorder.companyName }}
              </div>
              <div v-if="ordershowid === 9" style="
                  width: 60%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                <span class="label-style">购买单位</span>：{{ salesorder.company.companyName }}
              </div>
              <div v-if="ordershowid === 5" style="
                  width: 60%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                <span class="label-style">供货单位</span>：{{ salesorder.supplier.supplierName }}
              </div>

              <div v-if="ordershowid == 5 || ordershowid === 9" style="
                  width: 40%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                <span class="label-style">备注</span>：{{ salesorder.remarks }}
              </div>
              <div v-if="ordershowid == 4" style="
                  width: 28%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                订单编号:{{ salesorder.orderNumber }}
              </div>

            </div>
            <div style="width: 100%; display: flex; align-items: center">
              <div v-if="
                  ordershowid == 1 ||
                  ordershowid == 20 ||
                  ordershowid == 2 ||
                  ordershowid == 6 ||
                  ordershowid == 8
                " style="
                  width: 31%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                订单号：{{ salesorder.orderNumber }}
              </div>
              <div v-if="ordershowid == 3 || ordershowid == 11" style="
                  width: 31%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                采购单号：{{ salesorder.orderNumber }}
              </div>

              <div v-if="
                  ordershowid == 1 ||
                  ordershowid == 2 ||
                  ordershowid == 6 ||
                  ordershowid == 20
                " style="
                  width: 69%;
                  font-family: Microsoft YaHei;
                  color: black;
                  font-size: 15px;
                ">
                收货地址:{{ salesorder.receiveAddress }}
              </div>
            </div>
          </div>

          <table cellspacing="0" style="
              width: 96%;
              color: black;
              font-family: Microsoft YaHei;
              font-size: 16px;
              text-align: center;
              padding: 0 20px;
              margin: auto;
            ">
            <tr style="height: 35px; line-height: 35px">
              <td style="border: 1px solid black" v-if="ordershowid === 5 || ordershowid === 9">
                序号
              </td>
              <td style="border: 1px solid black" v-if="ordershowid === 5 || ordershowid === 9">
                商品编码
              </td>
              <td style="border: 1px solid black" v-if="ordershowid === 5 || ordershowid === 9">
                商品名称
              </td>
              <!-- <td style="border: 1px solid black">规格</td> -->
              <td style="border: 1px solid black" v-if="ordershowid === 5">单位</td>
              <td style="border: 1px solid black" v-if="ordershowid === 5 || ordershowid === 9">
                数量
              </td>
              <td style="border: 1px solid black" v-if="ordershowid === 5 || ordershowid === 9">
                单价
              </td>
              <td style="border: 1px solid black" v-if="
                  (ordershowid == 1 ||
                    ordershowid == 3 ||
                    ordershowid == 11 ||
                    ordershowid == 5 ||
                    ordershowid == 20 ||
                    ordershowid == 9 ||
                    ordershowid == 19) &&
                  sakeorderis != 1
                ">
                总金额
              </td>
              <td style="border: 1px solid black" v-if="
                  ordershowid != 4 &&
                  ordershowid != 8
                ">
                备注
              </td>
              <td style="border: 1px solid black" v-if="
                  ordershowid == 4 ||
                  ordershowid == 8
                ">
                实际数量
              </td>
            </tr>
            <tr v-for="(item, index) in salesorderlist" :key="item.id"
              style="height: 35px; line-height: 35px; text-align: center">
              <!-- //商品编码 -->
              <td style="border: 1px solid black; width: 5%">
                {{ index+1 }}
              </td>
              <td style="border: 1px solid black; width: 15%">
                {{ item.productNo || item.product?.productNo }}
              </td>

              <td style="border: 1px solid black; width: 10%" v-if="
                  ordershowid == 1 ||
                  ordershowid == 2 ||
                  ordershowid == 6 ||
                  ordershowid == 20 ||
                  ordershowid == 15 ||
                  ordershowid == 16
                ">
                {{ item.name }}
              </td>

              <td style="border: 1px solid black; width: 10%" v-if="ordershowid === 5 || ordershowid === 9">
                {{ item.productName || item.product?.productName }}
              </td>
              <td style="border: 1px solid black; width: 10%"
                v-if="ordershowid == 8 || ordershowid == 19">
                {{ item.originalName }}
              </td>
              <td style="border: 1px solid black; width: 10%" v-if="ordershowid == 4">
                {{ item.name }}
              </td>
              <td style="border: 1px solid black; width: 10%" v-if="ordershowid == 7">
                {{ item.proName }}
              </td>

              <!-- 规格 -->
              <!-- <td style="border: 1px solid black; width: 14%">
                {{ item.productSpec || item.product.productSpec }}
              </td> -->
              <!-- 单位 -->
              <td style="border: 1px solid black; width: 6%" v-if="ordershowid === 5">
                {{ item.productUnit || item.product?.productUnit }}
              </td>
              <!-- 数量 -->
              <td style="border: 1px solid black; width: 7%" v-if="ordershowid == 3 || ordershowid == 5 || ordershowid == 9">
                {{ item.number*1 }}
              </td>

              <td style="border: 1px solid black; width: 6%" v-if="ordershowid == 5 || ordershowid === 9">
                {{ item.price*1 }}
              </td>
              <!-- 箱数 -->
              <!-- <td style="border: 1px solid black;width: 5%" v-if="ordershowid == 1">
              {{ item.boxCountForOrder  }}
            </td> -->
              <!-- 总金额 -->
              <td style="border: 1px solid black; width: 8%" v-if="ordershowid === 5 || ordershowid === 9">
                {{ item.amount*1 }}
              </td>
              <!-- 备注 -->
              <td style="border: 1px solid black; width: 10%" v-if="
                  ordershowid != 4 &&
                  ordershowid != 8
                ">
                <div v-if="ordershowid == 3 || ordershowid == 11">
                  {{ item.productionremark }}
                </div>
                <div v-if="ordershowid === 5 || ordershowid === 9">
                  {{ item.remarks }}
                </div>

                <div v-if="ordershowid == 1 || ordershowid == 6 || ordershowid == 20">
                  {{ item.remark }}
                </div>

              </td>
            </tr>
            <tr v-if="ordershowid === 5 || ordershowid === 9" style="height: 35px; line-height: 35px; border: 1px solid black">
               <!-- colspan="5" -->
              <td style="border: 1px solid black">
                总计
              <!-- <th>
                <div style="
                      font-size: 16px;

                      height: 36px;
                      line-height: 36px;
                      display: flex;
                    ">
                  <div style="width: 25%" v-if="
                        ordershowid != 4 &&
                        ordershowid != 8 &&
                        ordershowid != 9 &&
                        sakeorderis != 33
                      ">
                    总数量：{{ totleNum }}
                  </div>
                  <div style="width: 40%" v-if="
                        ordershowid == 1 ||
                        ordershowid == 3 ||
                        ordershowid == 11 ||
                        ordershowid == 20 ||
                        (ordershowid == 5 && sakeorderis != 1)
                      ">
                    总计小写金额：{{ totleprice }}
                  </div>
                  <div style="width: 40%" v-if="
                        ordershowid == 1 ||
                        ordershowid == 3 ||
                        ordershowid == 11 ||
                        ordershowid == 20 ||
                        (ordershowid == 5 && sakeorderis != 1)
                      ">
                    总计大写金额：{{ showtotleprice }}
                  </div>
                </div>
              </th> -->
              </td>
              <td style="border: 1px solid black" v-if="ordershowid === 5 || ordershowid === 9"></td>
              <td style="border: 1px solid black" v-if="ordershowid === 5 || ordershowid === 9"></td>
              <td style="border: 1px solid black" v-if="ordershowid === 5"></td>
              <td style="border: 1px solid black" v-if="ordershowid === 5">{{totleNum}}</td>
              <td style="border: 1px solid black" v-if="ordershowid === 9">{{totleNum}}</td>
              <td style="border: 1px solid black" v-if="ordershowid === 5 || ordershowid === 9"></td>
              <td style="border: 1px solid black" v-if="ordershowid === 5 || ordershowid === 9">{{totleprice}}</td>
            </tr>
            <tr v-if="ordershowid === 9">
              <td style="border: 1px solid black"colspan="3"></td>
              <td style="border: 1px solid black" colspan="2">实收金额</td>
              <td style="border: 1px solid black" colspan="2"></td>
            </tr>
          </table>
          <div style="
              margin-top: 12px;
              color: black;
              font-size: 16px;
              font-family: Microsoft YaHei;
              padding: 0 10px;
            ">
            <div style="margin-left: 10px; display: flex; align-items: center;flex-wrap: wrap;">

              <div style="width: 60%" v-if="ordershowid === 5 || ordershowid === 9">
                <span class="label-style">经手人</span>：{{ salesorder.transactor }}
              </div>


              <div style="width: 40%" v-if="ordershowid === 5 || ordershowid === 9">
                <span class="label-style">录单员</span>：{{ salesorder.keyboarder }}
              </div>
              <div style="width: 60%; margin: 10px 0" v-if="ordershowid === 5 || ordershowid === 9">
                <span class="label-style">仓管</span>：{{ '--' }}
              </div>
              <div style="width: 40%; margin: 10px 0" v-if="ordershowid === 5 || ordershowid === 9">
                <span class="label-style">主管</span>：{{ salesorder.supervisor }}
              </div>
              <div style="width: 30%; margin: 10px 0" v-if="ordershowid == 5 && ssakeorderis == 33">
                <!-- v-if="
                (ordershowid == 3||  ordershowid == 11 && sakeorderis == 1) ||
                (ordershowid == 5 && ssakeorderis == 33)
              " -->
                生产日期：{{ salesorder.buyTime }}
              </div>

              <div style="width: 25%" v-if="
                  ordershowid == 1 || ordershowid == 2 || ordershowid == 6 || ordershowid == 7 || ordershowid == 20
                ">
                客户签字：
              </div>
              <div style="width: 25%; display: flex" v-if="ordershowid == 6 || ordershowid == 7">
                <div>配送方式：</div>
                <div v-if="salesorder.deliveryMethod == 1">自提</div>
                <div v-if="salesorder.deliveryMethod == 2">业务员自送</div>
                <div v-if="salesorder.deliveryMethod == 3">货拉拉</div>
                <div v-if="salesorder.deliveryMethod == 4">自有货车配送</div>
                <div v-if="salesorder.deliveryMethod == 5">工厂自送</div>
                <div v-if="salesorder.deliveryMethod == 6">快递</div>
                <div v-if="salesorder.deliveryMethod == 7">物流</div>
              </div>
              <div style="width: 25%; display: flex" v-if="ordershowid == 1 || ordershowid == 20">
                <div>发货方式：</div>
                <div v-if="salesorder.deliveryMethod == 1">自提</div>
                <div v-if="salesorder.deliveryMethod == 2">业务员自送</div>
                <div v-if="salesorder.deliveryMethod == 3">货拉拉</div>
                <div v-if="salesorder.deliveryMethod == 4">自有货车配送</div>
                <div v-if="salesorder.deliveryMethod == 5">工厂自送</div>
                <div v-if="salesorder.deliveryMethod == 6">快递</div>
                <div v-if="salesorder.deliveryMethod == 7">物流</div>
              </div>
              <div style="width: 25%; margin: 10px 0" v-if="ordershowid == 19">
                签字日期：{{ salesorder.buyTime }}
              </div>
            </div>


          </div>
        </div>
      </div>
      <el-divider></el-divider>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showprint = false">取消</el-button>
          <el-button type="primary" @click="comfigPrint" v-if="ordershowid != 20">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
  import html2canvas from "html2canvas";
  import printJS from "print-js";
  import QRCode from "qrcodejs2-fix";

  import {
    h,
    onMounted,
    reactive,
    ref,
    toRaw,
    nextTick,
    onUnmounted
  } from "vue";
  import {
    emitter
  } from "@/utils/mitt";
  import {
    ElMessageBox
  } from 'element-plus'
  const showprint = ref(false)
  const showtotleprice = ref()
  const salesorder = ref({})
  const componyname = ref()
  const originalProducts = reactive([{
      value: "浙江点对点科技有限公司",
      label: "811279574000190",
    },
    {
      value: "杭州华久新材料科技有限公司",
      label: "1202052719900042316",
    },
  ])
  const ssakeorderis = ref(0)
  const salesorderlist = ref([])
  const label = ref(1)
  const ordershowid = ref(0)
  const sakeorderis = ref(0)
  const checked1 = ref(false)
  const checked2 = ref(false)
  const totleprice = ref(0)
  const totleNum = ref(0)
  const ordername = ref()
  onMounted(() => {
    emitter.on('printinfos', (data) => {
      salesorder.value = []
      console.log("订单数据2", data);
      if (data.printinfoid != 5) {
        originalProducts.forEach((item) => {
          if (item.label == data.componname) {
            componyname.value = item.value;
            salesorder.value = [];
          }
        });
      } else {
        salesorder.value = [];
        componyname.value = data.componname;
      }

      salesorder.value = [];
      checked2.value = false;
      checked1.value = false;
      //data.printinfoid=1   销售单        2.发货单     3.采购单
      if (data.printinfoid === 1) {
        ordername.value = "销售单";
        ordershowid.value = data.printinfoid;
        totleprice.value = 0;
        salesorder.value = data.printinfos;
        salesorderlist.value = [];
        salesorderlist.value = data.printinfos.list;
        salesorderlist.value?.forEach((item) => {
          totleprice.value += item.totalPrice;
        });

        let realVal = parseFloat(totleprice.value).toFixed(2);

        totleprice.value = realVal;
        // console.log(parseFloat(64532.33333333333333).toFixed(2))
        showtotleprice.value = moneies(realVal);
        showprint.value = data.printinfosstuas;

        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
      } else if (data.printinfoid === 2) {
        //data.printinfoid       1销售单        2.发货单     3.采购单
        ordername.value = "发货单";
        ordershowid.value = data.printinfoid;
        totleprice.value = 0;
        salesorder.value = data.printinfos;
        salesorderlist.value = [];
        salesorderlist.value = data.printinfos.list;
        salesorderlist.value.forEach((item) => {
          totleprice.value += item.totalPrice;
        });
        showtotleprice.value = moneies(totleprice.value);
        showprint.value = data.printinfosstuas;
        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
      } else if (data.printinfoid == 3) {
        //data.printinfoid=1   销售单        2.发货单     3.采购单      4.生产单
        salesorder.value = [];
        ordername.value = "采购单";
        ordershowid.value = data.printinfoid;
        totleprice.value = 0;
        salesorder.value = data.printinfos;
        salesorderlist.value = [];
        // salesorderlist.push(data.printinfos);
        salesorderlist.value = data.printinfos.list;
        salesorderlist.value.forEach((item) => {
          totleprice.value += item.totalPrice;
        });

        let realVal = parseFloat(totleprice.value).toFixed(2);
        showtotleprice.value = moneies(realVal);
        // showtotleprice.value = moneies(totleprice.value);
        showprint.value = data.printinfosstuas;
        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
      } else if (data.printinfoid == 4) {
        //data.printinfoid=1   销售单        2.发货单     3.采购单     4.生产单
        salesorder.value = [];
        ordername.value = "生产单";
        ordershowid.value = data.printinfoid;
        totleprice.value = 0;
        salesorder.value = data.printinfos;
        salesorderlist.value = [];
        salesorderlist.value.push(data.printinfos);
        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
        showprint.value = data.printinfosstuas;
      } else if (data.printinfoid == 5) {
        salesorder.value = [];
        salesorderlist.value = [];
        showprint.value = data.printinfosstuas;
        ordername.value = "入库单";
        ordershowid.value = data.printinfoid;
        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
        ssakeorderis.value = 0;
        if (data.printinfos.status == 60) {
          ssakeorderis.value = 99;
        }
        salesorderlist.value = data.printinfos?.orderProducts ?? [];
        salesorder.value = data.printinfos;
        totleprice.value = 0;
        totleNum.value = 0
        // if (data.sakeorderis) {
        //   //仓库生产gsukyre
        //   ssakeorderis.value = 33;
        //   salesorderlist.value = [];
        //   sakeorderis.value = Number(data.sakeorderis);
        //   salesorderlist.value.push(data.printinfos);
        // }
        salesorderlist.value.forEach((item) => {
          totleprice.value += item.amount*1;
          totleNum.value += item.number*1;
        });

        let realVal = totleprice.value;
        showtotleprice.value = moneies(realVal);
      } else if (data.printinfoid == 6) {
        //6.送货单
        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
        salesorder.value = [];
        salesorderlist.value = [];
        ordername.value = "送货单";
        salesorder.value = data.printinfos;
        showprint.value = data.printinfosstuas;
        if (data.printinfosstuasid == 1) {
          //仓库送货单
          salesorderlist.value = data.printinfos?.list ?? [];
          ordershowid.value = data.printinfoid;
          console.log('xxxxxxx1===', ordershowid.value);
        } else {
          salesorderlist.value.push(data.printinfos);
          ordershowid.value = 7;
          console.log('xxxxxxx2===', ordershowid.value);
        }
      } else if (data.printinfoid == 8) {
        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
        salesorder.value = [];
        salesorderlist.value = [];
        salesorder.value = data.printinfos;
        ordername.value = "取料单";
        ordershowid.value = data.printinfoid;

        // salesorderlist = data.printinfos.originalList;
        Object.assign(salesorderlist.value, data.printinfos.originalList);
        if (data.printinfos.boxCount && data.printinfos.boxCount > 0) {
          let boxinfo = {
            codeNumber: data.printinfos.originalCodeNumber,
            originalName: data.printinfos.originalName,
            specs: data.printinfos.originalSpecs,
            count: data.printinfos.boxCount,
            unitType: data.printinfos.originalUnitType,

            reallyUsedCount: data.printinfos.boxCount,
          };
          salesorderlist.value.push(boxinfo);
        }

        salesorderlist.value.forEach((item) => {
          if (data.printinfos.status != 50) {
            item.reallyUsedCount = "";
          }
        });
        console.log(salesorderlist.value, "salesorderlist");
        showprint.value = data.printinfosstuas;
      } else if (data.printinfoid == 9) {
        salesorder.value = [];
        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
        salesorderlist.value = [];
        ordername.value = "出库单";
        salesorder.value = data.printinfos;
        ordershowid.value = data.printinfoid;
        salesorderlist.value = data.printinfos.orderProducts;
        showprint.value = data.printinfosstuas;
        totleprice.value = 0;
        totleNum.value = 0
        salesorderlist.value.forEach((item) => {
          totleprice.value += item.amount*1;
          totleNum.value += item.number*1;
        });
      } else if (data.printinfoid == 19) {
        salesorder.value = [];
        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
        ordername.value = "取料单";
        ordershowid.value = data.printinfoid;
        totleprice.value = 0;
        salesorder.value = data.printinfos;
        salesorderlist.value = [];
        salesorderlist.value = data.printinfos.originalList;
        showprint.value = data.printinfosstuas;
      } else if (data.printinfoid == 20) {
        ordername.value = "销售单"; //销售单预览
        ordershowid.value = data.printinfoid;
        totleprice.value = 0;
        salesorder.value = data.printinfos;
        salesorderlist.value = [];
        salesorderlist.value = data.printinfos.list;
        salesorderlist.value.forEach((item) => {
          totleprice.value += item.totalPrice;
        });

        let realVal = parseFloat(totleprice.value).toFixed(2);

        totleprice.value = realVal;
        // console.log(parseFloat(64532.33333333333333).toFixed(2))
        showtotleprice.value = moneies(realVal);
        showprint.value = data.printinfosstuas;

        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
      } else if (data.printinfoid == 11) {
        //data.printinfoid=1   销售单        2.发货单     3.采购单      4.生产单
        salesorder.value = [];
        ordername.value = "采购单";
        ordershowid.value = data.printinfoid;
        totleprice.value = 0;
        salesorder.value = data.printinfos;
        salesorderlist.value = [];
        // salesorderlist.value.push(data.printinfos);
        salesorderlist.value = data.printinfos.list;
        salesorderlist.value.forEach((item) => {
          totleprice.value += item.totalPrice;
        });

        let realVal = parseFloat(totleprice.value).toFixed(2);
        showtotleprice.value = moneies(realVal);
        // showtotleprice.value = moneies(totleprice.value);
        showprint.value = data.printinfosstuas;
        if (data.printinfos.billType == "增值发票") {
          checked2.value = true;
          checked1.value = false;
        } else if (data.printinfos.billType == "普通发票") {
          checked1.value = true;
          checked2.value = false;
        }
      } else if (data.printinfoid == 15) {
        ordername.value = "取料单";
        ordershowid.value = data.printinfoid;
        totleprice.value = 0;
        salesorder.value = data.printinfos;
        salesorderlist.value = [];
        salesorderlist.value.push(data.printinfos);
        showprint.value = data.printinfosstuas;
      } else if (data.printinfoid == 16) {
        ordername.value = "取料单";
        ordershowid.value = data.printinfoid;
        totleprice.value = 0;
        salesorder.value = data.printinfos;
        salesorderlist.value = [];
        salesorderlist.value.push(data.printinfos);
        showprint.value = data.printinfosstuas;
      }
    })
  })
  onUnmounted(()=> {
    emitter.off(['printinfos'])
  })
  const moneies = (money) => {
    if (!money) return;
    let moneyArr = parseFloat(money).toFixed(2).split("."); // 转成字符串；分割整数和小数
    money = moneyArr[0]
      ?.replace(/\b(0+)/g, "")
      .split("")
      .reverse(); //整数部分： 去掉头部的0, 000130130 => 130130；传成数组；倒置
    let digit = moneyArr[1]?.replace(/(0+)\b/g, "").split(""); //小数部分： 去掉后面的0, 20 => 2；传成数组；

    let resMoney = 0;

    const dic = {
      0: "零",
      1: "壹",
      2: "贰",
      3: "叁",
      4: "肆",
      5: "伍",
      6: "陆",
      7: "柒",
      8: "捌",
      9: "玖",
    };

    const dicUnit = {
      0: "",
      1: "拾",
      2: "佰",
      3: "仟",
    };

    const digitDicUnit = {
      0: "角",
      1: "分",
    };

    // 处理仟、佰、拾； ['1', '0', '0', '1'] => 叁仟零零壹
    const getInfo = (item) => {
      if (!item || item.length > 4) return;

      return item
        .map((subItem, index) => {
          if (subItem === "0") {
            return dic[0];
          }
          return `${dic[subItem]}${dicUnit[index]}`;
        })
        .reverse()
        .join("")
        .replace(/(零+)$/g, ""); //反转回来；拼接成字符串；去掉尾部的 零
    };

    // 处理整数部分
    switch (true) {
      case money.length < 5:
        resMoney = getInfo(money);
        break;
      case money.length < 9:
        resMoney = `${getInfo(money.slice(4, money.length))}万${getInfo(
          money.slice(0, 4)
        )}`;
        break;
      case money.length < 13:
        resMoney = `${getInfo(money.slice(8, money.length))}亿${getInfo(
          money.slice(4, 8)
        )}万${getInfo(money.slice(0, 4))}`;
        break;
    }

    resMoney = `${resMoney.replace(/零零零/g, "零").replace(/零零/g, "零")}`;

    let digitMoney;
    // 处理小数部分
    if (digit) {
      digitMoney = digit
        .map((digitItem, index) => {
          if (digitItem === "0") {
            return;
          }
          return `${dic[digitItem]}${digitDicUnit[index]}`;
        })
        .join("");
    }

    !!digitMoney
      ?
      (resMoney = `${resMoney ? `${resMoney}元` : resMoney}${digitMoney}`) :
      (resMoney = `${resMoney}元`);

    return resMoney;
  }
  const priqrcodes = ref()
  const creatQrCode = () => {
    console.log("打开了", salesorder.value.orderNumberSale);
    return new QRCode(
      priqrcodes.value,
      JSON.stringify(salesorder.value.orderNumberSale)
    );
  }
  const opendialog = () => {
    nextTick(() => {
      // priqrcodes.value.innerHTML = "";
      // this.creatQrCode()

      if (salesorder.value.orderNumberSale) {
        new QRCode(priqrcodes.value, {
          text: salesorder.value.orderNumberSale, // 需要转换为二维码的内容
          width: 100,
          height: 100,
          colorDark: "#000000",
          colorLight: "#ffffff",
          correctLevel: QRCode.CorrectLevel.H,
        });
      }
    });
  }
  const printArea = ref()
  const comfigPrint = () => {
    ElMessageBox.confirm(
        "此操作将打印订单, 是否继续?",
        "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
      .then(() => {
        if (ordershowid.value == 1 || ordershowid.value == 6) {
          let updateParams = {
            id: salesorder.value.id
          };
          if (ordershowid.value == 1) {
            updateParams.printStatus = 2; // 是否打印 1未打印 2已打印
          } else if (ordershowid.value == 6) {
            updateParams.printStatus2 = 2; // 是否打印 1未打印 2已打印
          }

        }

        // 转图片打印
        html2canvas(printArea.value, {
          allowTaint: false,
          useCORS: true,
          height: printArea.value.outerHeight,
          width: printArea.value.outerWidth,
          windowWidth: document.body.scrollWidth,
          windowHeight: document.body.scrollHeight,
        }).then((canvas) => {
          // let url = canvas.toDataURL('image/jpeg', 1.0)
          const url = canvas.toDataURL();
          // img = url;

          printJS({
            printable: url,
            type: "image",
            documentTitle: "",
            style: '@page {margin:auto;}',
          });

          // printJS({
          //   printable: "printArea",
          //   type: "html",
          //   scanStyles: false,
          //   documentTitle: "",
          //   // targetStyles: ['*'], // 打印内容使用所有HTML样式，没有设置这个属性/值，设置分页打印没有效果
          //   style: "@page {margin:0};",
          //   onBeforePrint: function() {},
          // });
        });
      })
  }
</script>
<style scoped lang="scss">
  .label-style {
    display: inline-block;
    width: 70px;
    text-align: right;
    // text-align: justify;
    // text-align-last: justify;
  }
</style>
